<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>VALUES</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.1.2 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SQL Commands"
HREF="sql-commands.html"><LINK
REL="PREVIOUS"
TITLE="VACUUM"
HREF="sql-vacuum.html"><LINK
REL="NEXT"
TITLE="PostgreSQL Client Applications"
HREF="reference-client.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2011-12-01T22:07:59"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.1.2 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="VACUUM"
HREF="sql-vacuum.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="PostgreSQL Client Applications"
HREF="reference-client.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="SQL-VALUES"
></A
>VALUES</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN76448"
></A
><H2
>Name</H2
>VALUES&nbsp;--&nbsp;compute a set of rows</DIV
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN76453"
></A
><H2
>Synopsis</H2
><PRE
CLASS="SYNOPSIS"
>VALUES ( <TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
> [, ...] ) [, ...]
    [ ORDER BY <TT
CLASS="REPLACEABLE"
><I
>sort_expression</I
></TT
> [ ASC | DESC | USING <TT
CLASS="REPLACEABLE"
><I
>operator</I
></TT
> ] [, ...] ]
    [ LIMIT { <TT
CLASS="REPLACEABLE"
><I
>count</I
></TT
> | ALL } ]
    [ OFFSET <TT
CLASS="REPLACEABLE"
><I
>start</I
></TT
> [ ROW | ROWS ] ]
    [ FETCH { FIRST | NEXT } [ <TT
CLASS="REPLACEABLE"
><I
>count</I
></TT
> ] { ROW | ROWS } ONLY ]</PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN76461"
></A
><H2
>Description</H2
><P
>   <TT
CLASS="COMMAND"
>VALUES</TT
> computes a row value or set of row values
   specified by value expressions.  It is most commonly used to generate
   a <SPAN
CLASS="QUOTE"
>"constant table"</SPAN
> within a larger command, but it can be
   used on its own.
  </P
><P
>   When more than one row is specified, all the rows must have the same
   number of elements.  The data types of the resulting table's columns are
   determined by combining the explicit or inferred types of the expressions
   appearing in that column, using the same rules as for <TT
CLASS="LITERAL"
>UNION</TT
>
   (see <A
HREF="typeconv-union-case.html"
>Section 10.5</A
>).
  </P
><P
>   Within larger commands, <TT
CLASS="COMMAND"
>VALUES</TT
> is syntactically allowed
   anywhere that <TT
CLASS="COMMAND"
>SELECT</TT
> is.  Because it is treated like a
   <TT
CLASS="COMMAND"
>SELECT</TT
> by the grammar, it is possible to use
   the <TT
CLASS="LITERAL"
>ORDER BY</TT
>, <TT
CLASS="LITERAL"
>LIMIT</TT
> (or
   equivalently <TT
CLASS="LITERAL"
>FETCH FIRST</TT
>),
   and <TT
CLASS="LITERAL"
>OFFSET</TT
> clauses with a
   <TT
CLASS="COMMAND"
>VALUES</TT
> command.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN76478"
></A
><H2
>Parameters</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
></DT
><DD
><P
>      A constant or expression to compute and insert at the indicated place
      in the resulting table (set of rows).  In a <TT
CLASS="COMMAND"
>VALUES</TT
> list
      appearing at the top level of an <TT
CLASS="COMMAND"
>INSERT</TT
>, an
      <TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
> can be replaced
      by <TT
CLASS="LITERAL"
>DEFAULT</TT
> to indicate that the destination column's
      default value should be inserted.  <TT
CLASS="LITERAL"
>DEFAULT</TT
> cannot
      be used when <TT
CLASS="COMMAND"
>VALUES</TT
> appears in other contexts.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>sort_expression</I
></TT
></DT
><DD
><P
>      An expression or integer constant indicating how to sort the result
      rows.  This expression can refer to the columns of the
      <TT
CLASS="COMMAND"
>VALUES</TT
> result as <TT
CLASS="LITERAL"
>column1</TT
>, <TT
CLASS="LITERAL"
>column2</TT
>,
      etc.  For more details see
      <A
HREF="sql-select.html#SQL-ORDERBY"
><I
><I
>ORDER BY</I
> Clause</I
></A
>.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>operator</I
></TT
></DT
><DD
><P
>      A sorting operator.  For details see
      <A
HREF="sql-select.html#SQL-ORDERBY"
><I
><I
>ORDER BY</I
> Clause</I
></A
>.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>count</I
></TT
></DT
><DD
><P
>      The maximum number of rows to return.  For details see
      <A
HREF="sql-select.html#SQL-LIMIT"
><I
><I
>LIMIT</I
> Clause</I
></A
>.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>start</I
></TT
></DT
><DD
><P
>      The number of rows to skip before starting to return rows.
      For details see
      <A
HREF="sql-select.html#SQL-LIMIT"
><I
><I
>LIMIT</I
> Clause</I
></A
>.
     </P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN76519"
></A
><H2
>Notes</H2
><P
>   <TT
CLASS="COMMAND"
>VALUES</TT
> lists with very large numbers of rows should be avoided,
   as you might encounter out-of-memory failures or poor performance.
   <TT
CLASS="COMMAND"
>VALUES</TT
> appearing within <TT
CLASS="COMMAND"
>INSERT</TT
> is a special case
   (because the desired column types are known from the <TT
CLASS="COMMAND"
>INSERT</TT
>'s
   target table, and need not be inferred by scanning the <TT
CLASS="COMMAND"
>VALUES</TT
>
   list), so it can handle larger lists than are practical in other contexts.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN76527"
></A
><H2
>Examples</H2
><P
>   A bare <TT
CLASS="COMMAND"
>VALUES</TT
> command:

</P><PRE
CLASS="PROGRAMLISTING"
>VALUES (1, 'one'), (2, 'two'), (3, 'three');</PRE
><P>

   This will return a table of two columns and three rows.  It's effectively
   equivalent to:

</P><PRE
CLASS="PROGRAMLISTING"
>SELECT 1 AS column1, 'one' AS column2
UNION ALL
SELECT 2, 'two'
UNION ALL
SELECT 3, 'three';</PRE
><P>

  </P
><P
>   More usually, <TT
CLASS="COMMAND"
>VALUES</TT
> is used within a larger SQL command.
   The most common use is in <TT
CLASS="COMMAND"
>INSERT</TT
>:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');</PRE
><P>
  </P
><P
>   In the context of <TT
CLASS="COMMAND"
>INSERT</TT
>, entries of a <TT
CLASS="COMMAND"
>VALUES</TT
> list
   can be <TT
CLASS="LITERAL"
>DEFAULT</TT
> to indicate that the column default
   should be used here instead of specifying a value:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO films VALUES
    ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes'),
    ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama', DEFAULT);</PRE
><P>
  </P
><P
>   <TT
CLASS="COMMAND"
>VALUES</TT
> can also be used where a sub-<TT
CLASS="COMMAND"
>SELECT</TT
> might
   be written, for example in a <TT
CLASS="LITERAL"
>FROM</TT
> clause:

</P><PRE
CLASS="PROGRAMLISTING"
>SELECT f.*
  FROM films f, (VALUES('MGM', 'Horror'), ('UA', 'Sci-Fi')) AS t (studio, kind)
  WHERE f.studio = t.studio AND f.kind = t.kind;

UPDATE employees SET salary = salary * v.increase
  FROM (VALUES(1, 200000, 1.2), (2, 400000, 1.4)) AS v (depno, target, increase)
  WHERE employees.depno = v.depno AND employees.sales &gt;= v.target;</PRE
><P>

   Note that an <TT
CLASS="LITERAL"
>AS</TT
> clause is required when <TT
CLASS="COMMAND"
>VALUES</TT
>
   is used in a <TT
CLASS="LITERAL"
>FROM</TT
> clause, just as is true for
   <TT
CLASS="COMMAND"
>SELECT</TT
>.  It is not required that the <TT
CLASS="LITERAL"
>AS</TT
> clause
   specify names for all the columns, but it's good practice to do so.
   (The default column names for <TT
CLASS="COMMAND"
>VALUES</TT
> are <TT
CLASS="LITERAL"
>column1</TT
>,
   <TT
CLASS="LITERAL"
>column2</TT
>, etc in <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>, but
   these names might be different in other database systems.)
  </P
><P
>   When <TT
CLASS="COMMAND"
>VALUES</TT
> is used in <TT
CLASS="COMMAND"
>INSERT</TT
>, the values are all
   automatically coerced to the data type of the corresponding destination
   column.  When it's used in other contexts, it might be necessary to specify
   the correct data type.  If the entries are all quoted literal constants,
   coercing the first is sufficient to determine the assumed type for all:

</P><PRE
CLASS="PROGRAMLISTING"
>SELECT * FROM machines
WHERE ip_address IN (VALUES('192.168.0.1'::inet), ('192.168.0.10'), ('192.168.1.43'));</PRE
><P></P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
>    For simple <TT
CLASS="LITERAL"
>IN</TT
> tests, it's better to rely on the
    list-of-scalars form of <TT
CLASS="LITERAL"
>IN</TT
> than to write a <TT
CLASS="COMMAND"
>VALUES</TT
>
    query as shown above.  The list of scalars method requires less writing
    and is often more efficient.
   </P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN76565"
></A
><H2
>Compatibility</H2
><P
><TT
CLASS="COMMAND"
>VALUES</TT
> conforms to the SQL standard.
   <TT
CLASS="LITERAL"
>LIMIT</TT
> and <TT
CLASS="LITERAL"
>OFFSET</TT
> are
   <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> extensions; see also
   under <A
HREF="sql-select.html"
>SELECT</A
>.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN76573"
></A
><H2
>See Also</H2
><A
HREF="sql-insert.html"
>INSERT</A
>, <A
HREF="sql-select.html"
>SELECT</A
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-vacuum.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="reference-client.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>VACUUM</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>PostgreSQL Client Applications</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>